package com.jyh.blog.controller;

import com.jyh.blog.annotation.AccessLimit;
import com.jyh.blog.dto.CommentBackDTO;
import com.jyh.blog.dto.CommentDTO;
import com.jyh.blog.dto.ReplyDTO;
import com.jyh.blog.service.CommentService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.CommentVO;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import com.jyh.blog.vo.ReviewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "评论模块")
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 查询评论
     * @param commentVO
     * @return
     */
    @ApiOperation(value = "查询首页评论")
    @GetMapping("/comments")
    public PageResult<CommentDTO> commentList(CommentVO commentVO){
        return commentService.commentList(commentVO);
    }

    /**
     * 添加评论
     * @param commentVO
     * @return
     */
    @AccessLimit(seconds = 15,maxCount = 1)
    @ApiOperation(value = "添加评论")
    @PostMapping("/comments")
    public Result saveComment(@Valid @RequestBody  CommentVO commentVO){
        commentService.saveComments(commentVO);
        return Result.ok();
    }

    /**
     * 评论点赞
     * @param commentId
     * @return
     */
    @ApiOperation(value = "评论点赞")
    @PostMapping("comment/{commentId}/like")
    public Result commentLike(@PathVariable("commentId") String commentId){
        commentService.commentLike(commentId);
        return Result.ok();
    }

    /**
     * 查询当前评论下的回复
     * @param commentId
     * @return
     */
    @ApiOperation(value = "查询当前评论下的回复")
    @GetMapping("comments/{commentId}/reply")
    public Result<List<ReplyDTO>> replyByCommentId(@PathVariable("commentId") String commentId){
        return Result.ok(commentService.replyByCommentId(commentId));
    }

    /**
     * 查询后台评论列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "查询后台评论")
    @GetMapping("admin/comments")
    public Result<PageResult<CommentBackDTO>> commentBackDTOList(ConditionVO conditionVO){
        return Result.ok(commentService.commentBackList(conditionVO));
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("admin/comments")
    public Result deleteCommentByIds(@RequestBody List<String> commentIds){
        commentService.deleteCommentByIds(commentIds);
        return Result.ok();
    }
    @ApiOperation(value = "审核评论")
    @PutMapping("admin/comments/review")
    public Result commentReview(@Valid @RequestBody ReviewVO reviewVO){
        commentService.commentReview(reviewVO);
        return Result.ok();
    }
}
